import api from "../../utils/api_util";
import baseUtil from "../../utils/base_util";
import tip from "../../utils/tip";

const utils = require('../../utils/util');
const url = require('../../utils/url.js');
const app = getApp()
Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        BannerSrc: ['/images/other/x4.jpg', '/images/other/x6.jpg', '/images/other/x8.jpg', '/images/other/x9.jpg'],
        MsgList: [{
            url: "url",
            title: "蜀粮科技组织架构调整"
        },
            {
                url: "url",
                title: "蜀粮科技组织架构调整"
            },
            {
                url: "url",
                title: "蜀粮科技组织架构调整蜀粮科技组织架构调整蜀粮科技组织架构调整"
            }
        ],
        NewShopList: [],
        RecomShopList: [],
        ReserShopList: [],
        //搜索字段
        searchKeyWord: '',
    },
    onShow: function () {
        this.getNewGoods();
        this.getRecomGoods();
        this.getReserGoods();
        this.getBanner();
        this.getPublicMsg();
    },
    //最新商品
    getNewGoods: function () {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.goodsList, {
            method: 'GET',
            data: {
                pageSize: 10,    //每页条数
                pageNumber: 1, //页码
                type: 2,
            },
            success: function (res) {
                console.log('res:',res);

                    that.setData({
                        NewShopList: res.data.list,
                    })

            },
            code:function (res) {
                if (res.code == '0101001' || res.code == '0101007' || res.code == '0101006') {
                    setTimeout(function () {
                        tip.alert("请先登录~");
                    }, 1000);
                    setTimeout(() => {
                        baseUtil.pageJump('../Login/Login', false)
                    }, 100);
                    console.log('跳登录')
                }
            }
        })
    },
    //推荐商品
    getRecomGoods: function () {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.goodsList, {
            method: 'GET',
            data: {
                pageSize: 10,    //每页条数
                pageNumber: 1, //页码
                type: 1,
            },
            success: function (res) {
                that.setData({
                    RecomShopList: res.data.list,
                })
            },
        })
    },
    //预定商品
    getReserGoods: function () {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.goodsList, {
            method: 'GET',
            data: {
                pageSize: 20,    //每页条数
                pageNumber: 1, //页码
                type: 3,
            },
            success: function (res) {
                that.setData({
                    ReserShopList: res.data.list,
                })
            },
        })
    },
    onReachBottom: function () {
        // var that = this;
        // utils.http(url.webhttp + url.setFav, 'GET', { page: ++this.data.page })
        //   .then(function (response) {
        //   }, function (error) {
        //     console.log(error);
        //   })
        //
        // this.setData({
        //   showLoading: true
        // })
        // setTimeout(function() {
        //   that.setData({
        //     showLoading: false
        //   })
        // }, 4000)
        // var that = this;
        // const openId = app.globalData.userOpenId;
        // util.http(url.ApiUrl + "DouWen/discoveryListLt/" + (++this.data.page) + "/" + openId, 'GET', {}, function (res) {
        //   var note = that.data.note;
        //   if (res.length == 0) {
        //     wx.showToast({
        //       title: '亲，木有数据了哦~！',
        //       icon: "none",
        //     });
        //   } else {
        //     wx.showLoading({
        //       title: '玩命加载中'
        //     })
        //     for (var i = 0; i < res.length; i++) {
        //       if (res[i].contentImgUrl.indexOf("http") < 0) {
        //         res[i].contentImgUrl = url.Host + util.urlCheck(res[i].contentImgUrl);
        //       }
        //       note.push(res[i]);
        //     }
        //     setTimeout(function () {
        //       that.setData({
        //         note: note,
        //       })
        //       wx.hideLoading();
        //     }, 2000)
        //   }
        // })
    },
    //页面跳转
    jumpPage(e) {
        let id = e.target.dataset.id,
            url = e.target.dataset.url,
            name = e.target.dataset.name,
            type = e.target.dataset.type;

        if (id && id > 0) {
            baseUtil.pageJump('../ShopDetail/ShopDetail?id=' + id, false);
        } else if (type) {
            baseUtil.pageJump(url + '?jumpType=' + name + '&postType=' + type, false);
        } else {
            baseUtil.pageJump(url + '?jumpType=' + name + '&keyword=' + this.data.searchKeyWord, false);
        }
    },
    //搜索字段
    searchInput: function (e) {
        this.data.searchKeyWord = e.detail.value;

        this.setData({
            searchKeyWord: this.data.searchKeyWord,
        })
    },
    showModal: function (e) {
        var showName = e.currentTarget.dataset.modal;
        this.setData({
            modalName: showName
        })
    },
    closeModal: function (e) {
        this.setData({
            modalName: null
        })
    },

    //banner
    getBanner: function () {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.bannerList, {
            method: 'GET',
            data: {
                position: 0,    //每页条数
            },
            success: function (res) {
                if(res.data && res.data.list.length > 0){
                    that.setData({
                        BannerSrc: res.data.list
                    })
                }
            },
        })
    },
    //点击banner判断操作
    bannerClickEvent: function (e) {
        let type = e.currentTarget.dataset.type;
        if (type == '0') {
            console.log('直接跳转url')

        } else if (type == '1') {
            console.log('跳转文章详情')
        } else if (type == '2') {
            console.log('店铺')
        } else if (type == '3') {
            console.log('商品详情')
        } else if (type == '4') {
            console.log('纯图片,没操作')
        }
    },
    //公告
    getPublicMsg: function () {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.informationList, {
            method: 'GET',
            data: {
                categoryId: 1,
                position: '',
                pageNumber: '',    //每页条数
                pageSize: '',    //每页条数
            },
            success: function (res) {
                that.setData({
                    MsgList: res.data.rows
                })
            },
        })
    },
})